package editeurGraphiti.features;

import modeleProcessus.ElementNomme;

import org.eclipse.graphiti.examples.common.ExampleUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.ICustomContext;
import org.eclipse.graphiti.features.custom.AbstractCustomFeature;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;

public class RenameElementFeature extends AbstractCustomFeature {

	public RenameElementFeature(IFeatureProvider fp) {
		super(fp);
	}

	private boolean hasDoneChanges = false;

	@Override
	public String getName() {
		return "Renommer un element";
	}

	@Override
	public String getDescription() {
		return "Changer le nom";
	}

	@Override
	public boolean canExecute(ICustomContext context) {
		// allow rename if exactly one pictogram element
		// representing a EClass is selected
		boolean ret = false;
		PictogramElement[] pes = context.getPictogramElements();
		if (pes != null && pes.length == 1) {
			Object bo = getBusinessObjectForPictogramElement(pes[0]);
			if (bo instanceof ElementNomme) {
				ret = true;
			}
		}
		return ret;
	}

	@Override
	public void execute(ICustomContext context) {

		PictogramElement[] pes = context.getPictogramElements();
		if (pes != null && pes.length == 1) {
			Object bo = getBusinessObjectForPictogramElement(pes[0]);
			if (bo instanceof ElementNomme) {
				ElementNomme element = (ElementNomme) bo;
				String currentName = element.getNomElement();
				// ask user for a new class name
				String newName = ExampleUtil.askString(getName(),
						getDescription(), currentName);
				if (newName != null && !newName.equals(currentName)) {
					this.hasDoneChanges = true;
					element.setNomElement(newName);
					updatePictogramElement(pes[0]);
				}
			}
		}
	}

	@Override
	public boolean hasDoneChanges() {
		return this.hasDoneChanges;
	}

}
